//
//  HiDateView.m
//  HiCar
//
//  Created by huachun on 2019/1/11.
//  Copyright © 2019 丁一. All rights reserved.
//

#import "HiDateView.h"

@implementation HiDateView

-(instancetype)initWithFrame:(CGRect)frame type:(NSInteger)type{
    if (self=[super initWithFrame:frame]) {
        
        if (!self.buttonbg) {
            self.buttonbg = [[UIButton alloc] initWithFrame:RECR];
            self.buttonbg.backgroundColor=[UIColor blackColor];
            self.buttonbg.alpha=0.0;
            [self.buttonbg addTarget:self action:@selector(hidehere) forControlEvents:UIControlEventTouchUpInside];
        }
        
        self.buttondone = [[UIButton alloc] initWithFrame:CGRectMake(10, 210, RECRWITCH-20, 50)];
        self.buttondone.backgroundColor=[UIColor whiteColor];
        [self.buttondone addTarget:self action:@selector(hidehere) forControlEvents:UIControlEventTouchUpInside];
        self.buttondone.layer.cornerRadius=10;
        self.buttondone.layer.masksToBounds=YES;
        [self.buttondone setTitle:@"完  成" forState:UIControlStateNormal];
        [self.buttondone setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [self.buttondone.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
        [self addSubview:self.buttondone];
        
        
        NSCalendar *calendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        NSDateComponents *comps = [[NSDateComponents alloc] init];
        NSInteger unitFlags = NSCalendarUnitEra |
        NSCalendarUnitYear |
        NSCalendarUnitMonth |
        NSCalendarUnitDay |
        NSCalendarUnitHour |
        NSCalendarUnitMinute |
        NSCalendarUnitSecond |
        NSCalendarUnitWeekOfYear |
        NSCalendarUnitWeekday |
        NSCalendarUnitWeekdayOrdinal |
        NSCalendarUnitQuarter;
        comps = [calendar components:unitFlags fromDate:[NSDate date]];
        
        NSDateComponents * dateComps = [[NSDateComponents alloc] init];
        [dateComps setYear:-100];
        NSDate * mindata = [calendar dateByAddingComponents:dateComps toDate:[NSDate date] options:0];
        
        self.datePicker = [[UIDatePicker alloc]init];
        self.datePicker.backgroundColor = [UIColor whiteColor];
        self.datePicker.layer.cornerRadius=10;
        self.datePicker.layer.masksToBounds=YES;
        self.datePicker.frame = CGRectMake(10, 0, RECRWITCH-20, 200);
        self.datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
        [self.datePicker setDate:[NSDate date] animated:YES];
        if (type==0){
            self.datePicker.datePickerMode = UIDatePickerModeDate;
            [self.datePicker setMaximumDate:[[calendar dateFromComponents:comps] dateByAddingTimeInterval:-24*3600*365*1]];
            [self.datePicker setMinimumDate:mindata];
            self.datePicker.date=[[calendar dateFromComponents:comps] dateByAddingTimeInterval:-24*3600*365*18];
        }else{
            self.datePicker.datePickerMode = UIDatePickerModeDateAndTime;
            [self.datePicker setMaximumDate:[[calendar dateFromComponents:comps] dateByAddingTimeInterval:24*3600*90]];
            [self.datePicker setMinimumDate:[NSDate date]];
        }

        self.datePicker.timeZone = [NSTimeZone systemTimeZone];
        [self.datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
        [self.datePicker setValue:[UIColor blackColor] forKey:@"textColor"];
        
        
        [self addSubview:self.datePicker];
    
        
    }
    return self;
}

-(void)datePickerValueChanged:(UIDatePicker *)picker{
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
    
    [dateformatter setDateFormat:@"yyyy年MM月dd日"];
    
    NSString *dateStr = [dateformatter stringFromDate:picker.date];
    
    _selectblock (dateStr,picker.date);
}

- (void)hidehere{
    _hideblock();
}

@end
